//
//  MyIntegralChangNotes.swift
//  甜言蜜语
//
//  Created by yingli_yanfa_liuxingyu on 16/9/20.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
protocol MyIntegralChangNotesDelegate {
    func integralBtnSelected()
    func integralNumSelected()
    func changeNotesSlected()
}
class MyIntegralChangNotes: UIView {

    @IBOutlet weak var integralBtn: UIButton!
    @IBOutlet weak var integralNum: UIButton!
    @IBOutlet weak var changeNotes: UIButton!
    @IBOutlet var customView: UIView!
    var maxHeight: CGFloat = 0
    var delegate: MyIntegralChangNotesDelegate?
    var type: String?{
        didSet{
            
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.commonInit()
    }
    
    
    func commonInit () {
        NSBundle.mainBundle().loadNibNamed("MyIntegralChangNotes", owner: self, options: nil)
        self.customView.frame.size.width = 111
        self.customView.width(self.width)
        self.customView.height(132)
        self.customView.top(0)
        self.customView.frame.origin.x = SCREEN_WIDTH - 111
        self.backgroundColor = UIColor.clearColor()
        self.alpha = 0
        
        
        let maskView:UIView = UIView.init(frame: self.bounds)
        maskView.backgroundColor = UIColor.blackColor()
        maskView.alpha = 0.5
        self.addSubview(maskView)
        
        self.addSubview(self.customView)
        let tap = UITapGestureRecognizer.init(target: self, action:#selector(MyIntegralChangNotes.tapClick(_:)))
        maskView.addGestureRecognizer(tap)
        self.viewFrame()
        DataTool.getPersonIntegralNum({ (newModel) in
            guard newModel != nil else {
                return
            }
            
            self.integralBtn.setTitle(String(newModel!.integral), forState: UIControlState.Normal)
        })

        
    }
    
    func tapClick (sender:UITapGestureRecognizer) {
        self.closeBtnClick()
    }
    
    //MARK:view的位置
    func viewFrame() {
        
        if maxHeight == kScreen_height {
            UIView.animateWithDuration(0.25, animations: { () -> Void in
                self.customView.frame.origin.y = kScreen_height
                self.alpha = 0
            })
        } else {
            UIView.animateWithDuration(0.25, animations: { () -> Void in
                self.alpha = 1
                self.customView.top(0)
            })
        }
    }
    
    
    func closeBtnClick(){
        maxHeight = kScreen_height
        self.viewFrame()
        
    }

    @IBAction func integralBtnClicked(sendedr: UIButton){
        delegate?.integralBtnSelected()
        self.closeBtnClick()
    }
    @IBAction func integralNum(sender: UIButton){
        delegate?.integralNumSelected()
        self.closeBtnClick()
    }
    @IBAction func changeNotes(sender: UIButton){
        delegate?.changeNotesSlected()
        self.closeBtnClick()
    }
}
